<?php

	class GenerateViewFilesCommand implements ICommand
	{
		public function execute($event)
		{
			$fields = $event->data['fields'];
			$module = $event->data['module'];
			$name 	= $event->data['name'];			
			
			$viewProfileBuffer 	= '<?php
	$'.$name.'_model = EbModel::getModel(\''.$module.'\',\''.$name.'\');
	$'.$name.' = $'.$name.'_model->selectById_1(1);
	
	if (is_null($'.$name.'))
	{
		echo "Not Found!";
	}
	else
	{			
		?>
		<h2>$name</h2>
		<table>';
			
			$viewEditBuffer 	= '<?php
	$fields = array();
	$id		= EbController::$request->getData("id");
	
	if (!is_null($id))
	{
		$'.$name.'_model = EbModel::getModel(\''.$module.'\',\''.$name.'\');
		$'.$name.' = $'.$name.'_model->selectById_1($id);
	}

';

			$viewListingBuffer	= '';
			
			foreach ($fields AS $field)
			{
				$viewEditBuffer .= "\n\t".'$fields'." []= (object) array('label' => '$field','name' => '".strtolower($field)."');";
			
				$viewProfileBuffer .= "\n\t\t\t".'<tr><td>'.$field.'</td><td><?php echo $'.$name.'->'.strtolower($field).'; ?></td></tr>';
			}
			
			$viewProfileBuffer .= "\n\t\t</table>\n<?php\n\t}\n?>";
			
			$viewEditBuffer .= '

	if (isset($'.$name.'))
	{';
	
			for ($i = 0; $i < count($fields); $i++)
			{
				$viewEditBuffer .= "\n\t\t".'$fields['.$i.']->value = $'.$name.'->'.strtolower($fields[$i]).';';
			}			
			
	$viewEditBuffer .= '
	}
		
	eb_widget(\'form\',array(\'action\' => \'/'.$module.'/Edit'.$name.'.do\',\'fields\' => $fields));

?>';
	
			$modulefile_model = EbModel::getModel('Core','ModuleFile');	
			
			$modulefile_model->insert((object) array('module' => $module,'filename' => $name.'Profile','type' => 'Component','content' => $viewProfileBuffer));
			$modulefile_model->insert((object) array('module' => $module,'filename' => $name.'Edit','type' => 'Component','content' => $viewEditBuffer));
				
			return 'succes';
		}
	}